/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.cezary.butler.walili.server.db.tasks;

import pl.cezary.butler.walili.common.User;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * HSQL DB COMPAT
 *
 * @author Przemysław Bałdyga
 */
public class GetIntUserIdTask extends FetchingTask<Integer> {
//todo obsługa błledów

    private final String LOGIN;
    private int userId = -1;
    private static final Logger log = Logger.getLogger("ZadanieIsUser");

    public GetIntUserIdTask(User user) {
        LOGIN = user.getLogin();
    }

    @Override
    public void executeInternal() {
        userId = getJdbcTemplate().queryForIntIfEmpty("SELECT id_user FROM users WHERE login LIKE ?", LOGIN);

        if (userId == 0) {
            log.log(Level.INFO, "Brak u\u017cytkownika:{0}", LOGIN);
        }
    }

    //zwracanie obiektu user na podstawie user jest bez sensu
    @Override
    public Integer $getResultsNonLocking() {
        return (userId >0) ? userId : null;
    }
}
